package com.anche.demo.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.anche.demo.entity.User;
import com.anche.demo.mapper.UserMapper;
import com.anche.demo.service.impl.UserService;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class UserListener extends AnalysisEventListener<User> {
    private UserService userService;
    private List<User> userList = new ArrayList<>();

    public UserListener() {
    }

    public UserListener(UserService userService) {
        this.userService = userService;
    }

    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 2000;

    //  一行行读取excel内容，然后用MybatisPlus的方法进行导入
    @Override
    public void invoke(User user, AnalysisContext analysisContext) {
        userList.add(user);
        if (userList.size() >= BATCH_COUNT) {
//            userService.saveBatch(userList);
            userList.clear();
        }
    }

    //  读取表头内容，导出可用到
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头：" + headMap);
    }

    //  读取完成之后进行的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        userService.saveBatch(userList);
    }
}
